package com.special.base;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;

public abstract class BaseFrameActivity extends FragmentActivity {
	public abstract void initialize();

	public static final int LOAD_DLG = 1;
	public static final int Exit_DLG = 2;
	private static final int MAX_PROGRESS = 100;
	private float progress = 0;
	/*
	 * 按back键一次设置为true，超时后重新设置为false，超时前再按backkey退出
	 */
	private boolean backkey_timeout = false;

	@Override
	@Deprecated
	protected final Dialog onCreateDialog(int id) {
		switch (id) {
		case LOAD_DLG:
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			dialog.setTitle("Loading");
			dialog.setMessage("Please Wait...");
			return dialog;
		case Exit_DLG:
			return new AlertDialog.Builder(this)
					.setTitle("Exit")
					.setMessage("Are you sure to exit?")
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									Application.getTopActivity().finish();
								}
							}).setNegativeButton("Cancel", null).create();
		default:
			return super.onCreateDialog(id);
		}
	}

	@SuppressWarnings("deprecation")
	protected void showDialog(int id, float progress) {
		switch (id) {
		case LOAD_DLG:
			this.progress = progress;
		}
		showDialog(id);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			showDialog(Exit_DLG);
			break;
		}
		return true; //true denotes that NO need to play another function named onKeyDown;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	@Deprecated
	protected final void onPrepareDialog(int id, Dialog dialog) {
		if (dialog.isShowing()) {
			dialog.dismiss();
		}
		switch (id) {
		case LOAD_DLG:
			ProgressDialog dlg = (ProgressDialog) dialog;
			dlg.incrementProgressBy(-dlg.getProgress());
			dlg.incrementProgressBy((int) (progress * MAX_PROGRESS));
		}
		super.onPrepareDialog(id, dialog);
	}

	@Override
	protected void onResume() {
		Application.setTopActivity(this);
		super.onResume();
	}

}
